#ifndef __TRANSPARENCIA_H__
#define __TRANSPARENCIA_H__

#include <string.h>
#include <math.h>
#include <sdl.h>
#include <sstream>
#include <iostream>

class Transparencia {
	
private:

	Uint8 red, green, blue;
	int rango;

public:

	// Constructor con valores por defecto
	Transparencia(){
		this->red = 255;
		this->green = 0;
		this->blue = 255;
		this->rango = 10;
	}

	// Constructor con valores pasados por parametro
	Transparencia(Uint8 r, Uint8 g, Uint8 b, int rango){
		this->red = r;
		this->green = g;
		this->blue = b;
		this->rango = rango;
	}

	// Constructor con un string descriptor de la transparencia como parametro
	// alpha tiene el formato #RRGGBB. Si alpha viene incompleto, entonces se crea la Transparencia
	// con el color por defecto (R=255,G=0,B=255)
	Transparencia(string alpha) {

		if (alpha.length() < 7) {
			this->red = 255;
			this->green = 0;
			this->blue = 255;
			this->rango = 10;
		} else {

			if(alpha.length() == 9) {
				string rangoString = alpha.substr(7,2); 
				std::stringstream ssRango;
				ssRango << std::hex << rangoString;
				ssRango >> this->rango ;
			} else {
				this->rango = 10;
			}

			string redString = alpha.substr(1,2);
			string greenString = alpha.substr(3,2);
			string blueString = alpha.substr(5,2);
			
			int r, g, b;

			// Se pasan los string de HEXA a INT
			std::stringstream ssRed;
			std::stringstream ssGreen;
			std::stringstream ssBlue;
			ssRed << std::hex << redString;
			ssRed >> r;
			ssGreen << std::hex << greenString;
			ssGreen >> g;
			ssBlue << std::hex << blueString;
			ssBlue >> b;

			this->red = r;
			this->green = g;
			this->blue = b;

		}

		
	}

	~Transparencia() {}


	int getRango(){
		return this->rango;
	}

	Uint8 getRed(){
		return this->red;
	}

	Uint8 getGreen(){
		return this->green;
	}

	Uint8 getBlue(){
		return this->blue;
	}

	void setRed(Uint8 redInt) {
		this->red = redInt;
	}

	void setBlue(Uint8 blueInt) {
		this->blue = blueInt;
	}

	void setGreen(Uint8 greenInt) {
		this->green = greenInt;
	}

	/*
	 * Metodo que decide si una pixel es transparentable o no a traves del siguiente criterio:
	 * si |Rpixel - Rtransparencia| + |Gpixel - Gtransparencia| + |Bpixel - Btransparencia| <= RANGO,
	 * entonces es transparentable, en caso contrario el pixel no es transparentable
	 */
	bool esPixelTransparentable(Uint8 red, Uint8 green, Uint8 blue) {

		double rangoR = red - this->red;
		double rangoG = green - this->green;
		double rangoB = blue - this->blue;
		double rango = fabs(rangoR) + fabs(rangoG) + fabs(rangoB);

		if (rango <= this->rango) {
			return true;
		}

		return false;
	}


};



#endif